//
//  Extension+UIButton.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/23.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit
extension UIButton {
    
    /// 创建 button 的主方法
    ///
    /// - parameter setImgName:     图片名字
    /// - parameter setBackImgName: 背景图片
    /// - parameter target:          target
    /// - parameter action:         action
    ///
    /// - returns:  返回一个按钮
    convenience init(setImgName: String, setBackImgName: String, target: Any?, action: Selector) {
        self.init()
        
        //添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        //设置背景图片
        self.setBackgroundImage(UIImage(named: setBackImgName), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named: "\(setBackImgName)_highlighted"), for: UIControlState.highlighted)
        //设置图片
        self.setImage(UIImage(named: setImgName), for: UIControlState.normal)
        self.setImage(UIImage(named: "\(setImgName)_highlighted"), for: UIControlState.highlighted)
        //设置按钮的大小,注意一定要设置按钮的大小,否则,按钮显示不出来
        self.sizeToFit()
        
    }
    
    /// 创建 item 的主方法
    ///
    /// - parameter setHighlightedImgName: 图片名字
    /// - parameter target:                 target
    /// - parameter action:                action
    ///
    /// - returns: 返回一个带有高亮图片的按钮
    convenience init(setHighlightedImgName: String? = nil,title: String? = nil, target: Any?, action: Selector) {
        self.init()
        
        //添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        if let img = setHighlightedImgName {
            
            //设置图片
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        if let tit = title {
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(SinaColor, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        //设置按钮的大小,注意一定要设置按钮的大小,否则,按钮显示不出来
        self.sizeToFit()
    }
 
    convenience init(setBackImgName: String, title: String, fontSize: CGFloat, titleColor: UIColor, target: Any?, action: Selector) {
        self.init()
        
        //添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        //设置背景图片
        self.setBackgroundImage(UIImage(named: setBackImgName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        //设置按钮的大小,注意一定要设置按钮的大小,否则,按钮显示不出来
        self.sizeToFit()
        
    }
}
